package com.yunfei.api.v2;

import com.yunfei.api.v1.magerIndcInfo.service.MagerIndcInfoService;
import com.yunfei.api.v2.contants.ImagesContant;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.*;
import com.yunfei.api.v2.result.ContractStsResult;
import com.yunfei.api.v2.result.ElecHeTongNoResult;
import com.yunfei.api.v2.result.GetAccountInfo;
import com.yunfei.api.v2.result.addAccountInfo.AddAccountInfoResultModel;
import com.yunfei.api.v2.result.createPersonInfo.CreatePersonInfoJsonRootBean;
import com.yunfei.api.v2.result.tunRegister.TurnRegisterResult;
import com.yunfei.api.v2.service.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.mapper.crm.TunRegisterMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 填写收益账号信息
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v2/accountInfo")
@Api(value = QueryHeTongSignInfoContant.ACCOUNTINFO_NAMESPACE,tags =  QueryHeTongSignInfoContant.ACCOUNTINFO_NAMESPACE )
public class AccountInfoApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;
    @Autowired
    private MagerIndcInfoMapper magerIndcInfoMapper;
    @Autowired
    private MagerIndcInfoTemplateMapper magerIndcInfoTemplateMapper;
    @Autowired
    private MagerIndcInfoService magerIndcInfoService;
    @Autowired
    private HTSGetStatusService htsGetStatusService;
    @Autowired
    private HTSGetAccountInfoService htsGetAccountInfoService;
    @Autowired
    private HTSAddAccountService htsAddAccountService;
    @Autowired
    private HTYuYueInfoService htYuYueInfoService;
    @Autowired
    private GetElecHeTongNoService getElecHeTongNoService;
    @Autowired
    private TunRegisterMapper tunRegisterMapper;
    @Autowired
    private CreatePersonInfoService createPersonInfoService;
    @Autowired
    private ElecContCustAddInfoMapper elecContCustAddInfoMapper;
    @Autowired
    private QueryUserNoService queryUserNoService;
    @Autowired
    private CaRenZhengService caRenZhengService;
    @Autowired
    private AccountInfoMapper accountInfoMapper;
    /**
     * @Description: 填写账户信息（投资者）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/add")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.ADD_ACCOUNT_INFO,notes = QueryHeTongSignInfoContant.ADD_ACCOUNT_INFO,response = AddAccountInfoResultModel.class)
    @ResponseBody
    public Object addAccount(AddAccountParam param,CustNoParam custNoParam,LoginMobileParam loginMobileParam){
        //填写完预约信息，需要调用创建个人客户接口
        ResultModel resultModel=null;
        String loginMobile=loginMobileParam.getLoginMobile();
        try{
            //电子合同签署客户录入账号时判断
            String reserveNo=param.getcReserveNo();
            String bankNo=param.getBankNo();//银行编号
            String bankAcco=param.getAcctNo();//账号信息
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("reserveNo",reserveNo);
            List<GetAccountInfo> liCaiShiLuRuInfoList=htsAddAccountService.getLiCaiShiLuRuInfo(queryMap);
            if(!StringUtil.isNullOrEmpty(liCaiShiLuRuInfoList)&&liCaiShiLuRuInfoList.size()>1){
                resultModel.setCode(GetAccountInfoEnum.NOT_MORE_ACCT_NO.getCode());
                resultModel.setMessage(GetAccountInfoEnum.NOT_MORE_ACCT_NO.getMessage());
                return resultModel;
            }else{
                if(!StringUtil.isNullOrEmpty(liCaiShiLuRuInfoList)){
                    GetAccountInfo liCaiShiLuRuInfo=liCaiShiLuRuInfoList.get(0);
                    if(!StringUtil.isNullOrEmpty(liCaiShiLuRuInfo)){
                        String luruBankNo=liCaiShiLuRuInfo.getBankCode();
                        String luruBankAcco=liCaiShiLuRuInfo.getBankAcco();
                        boolean isYiZhiFlg=true;
                        if(bankNo.equals(luruBankNo)&&luruBankAcco.equals(bankAcco)){
                            //客户选择的 银行编号 和 账号进行核对，并且一致
                            isYiZhiFlg=true;
                        }else{
                            isYiZhiFlg=false;
                        }
                        if(!isYiZhiFlg){
                            resultModel.setCode(GetAccountInfoEnum.ACCT_NO_YIZHINULL.getCode());
                            resultModel.setMessage(GetAccountInfoEnum.ACCT_NO_YIZHINULL.getMessage());
                            return resultModel;
                        }else{
                            resultModel=htsAddAccountService.add(param);
                            String custNo=custNoParam.getcCustNo();
                            if(StringUtil.isNullOrEmpty(custNo)){
                                resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
                                resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
                                return resultModel;
                            }
                            //保存成功后，为客户进行caR恩正
                            caRenZhengService.caRenZheng(resultModel,custNo,loginMobile,"");
                        }
                    }
                }

            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 编辑账户信息（投资者）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/update")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.UPDATE_ACCOUNT_INFO,notes = QueryHeTongSignInfoContant.UPDATE_ACCOUNT_INFO,response = AddAccountInfoResultModel.class)
    @ResponseBody
    public Object update(UpdateAccountParam param,CustNoParam custNoParam,LoginMobileParam loginMobileParam){
        //更新账号信息
        ResultModel resultModel=htsAddAccountService.update(param);
        //保存成功后，为客户进行caR恩正
        caRenZhengService.caRenZheng(resultModel,custNoParam.getcCustNo(),loginMobileParam.getLoginMobile(),"");
        return resultModel;
    }

    /**
     * @Description: 是否可以编辑
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/isEdite")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.IS_UPDATE,notes = QueryHeTongSignInfoContant.IS_UPDATE,response = ContractStsResult.class)
    @ResponseBody
    public Object isEdite(ReserveNoParam param){
        return htsGetAccountInfoService.isEdite(param);
    }
    /**
     * @Description: 是否可以编辑
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/isShiMingRenZheng")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.IS_RENZHENG,notes = QueryHeTongSignInfoContant.IS_RENZHENG_NOTE)
    @ResponseBody
    public Object isShiMingRenZheng(CustNoParam custNoParam,EntreMobileParam mobileParam){
        return htsGetAccountInfoService.isShiMingRenZheng(custNoParam,mobileParam);
    }

    /**
     * @Description: 查询受益账号信息
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/get")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_INFO,notes = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_INFO_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getShouYiZhangHaoInfo(CustNoParam param,ReserveNoParam yueYuParam){
        return htsGetAccountInfoService.getAccountInfo(param,yueYuParam);
    }
    /**
     * @Description: 受益账号选择
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/select")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_SELECT,notes = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_SELECT,response = GetAccountInfo.class)
    @ResponseBody
    public Object getShouYiZhangHaoSelect(CustNoParam param){
        return htsGetAccountInfoService.getAccountSelect(param);
    }

    /**
     * @Description: 根据银行卡号自动获取银行信息
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/8/12
     * @time:10:31
     */
    @PostMapping(value="/getBankByNo")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_BANK,notes = QueryHeTongSignInfoContant.GET_BANK_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getBankByNo(BankNoParam param){
        return htsGetAccountInfoService.getBankByNo(param);
    }

    /**
     * @Description: 根据银行卡号自动获取银行信息
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/8/12
     * @time:10:31
     */
    @PostMapping(value="/getBankList")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_BANK_LIST,notes = QueryHeTongSignInfoContant.GET_BANK_LIST_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getBankList(){
        return htsGetAccountInfoService.getBankList();
    }
    /**
     * @Description: 根据银行卡号自动获取银行信息
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/8/12
     * @time:10:31
     */
    @PostMapping(value="/getIdenNo")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_IDENNO_API+ ImagesContant.IMG_STR,notes = QueryHeTongSignInfoContant.GET_IDENNO_API+ ImagesContant.IMG_STR)
    @ResponseBody
    public Object getIdenNo(LoginMobileParam loginMobileParam,CustNoParam custNoParam){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> idenNoQueryMap=new HashMap<>();
        String mobile=loginMobileParam.getLoginMobile();
        String custNo=custNoParam.getcCustNo();
        idenNoQueryMap.put("mobile",mobile);
        idenNoQueryMap.put("custId",custNo);
        String idenNo=accountInfoMapper.getIdenNoByLoginMobile(idenNoQueryMap);
        resultModel.setData(idenNo);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        return resultModel;
    }
}
